home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / dateimanager / browserii_v3_09 / sources / addicon.c next >
C/C++ Source or Header  |  1996-04-07  |  7KB  |  243 lines

  1. /*
  2.  *  AddIcon.c - Copyright © 1994 by S.R. & P.C.
  3.  *
  4.  *    Created:    23 May 1992  12:00:40
  5.  *  Modified:   18 Dec 1994  16:59:33
  6.  *
  7.  *  Make>> rx BumpRev AddIcon 1 3
  8.  *  Make>> sc <file>.c
  9.  *  Make>> slink LIB:cs.o <file>.o SC SD ND BATCH NOICONS TO <file> LIB LIB:c.lib
  10.  */
  11.  
  12. #include <libraries/WhatIsBase.h>
  13. #include <proto/WhatIs.h>
  14. #include <clib.h>
  15. #include "AddIcon_rev.h"
  16.  
  17. // Startup Modules...
  18. CliArgs; 
  19.  
  20. struct Library *WhatIsBase;
  21. struct Library *IconBase;
  22.  
  23. #define MAXFULLNAMELEN 255
  24.  
  25. char *Template = "FileName/A,ONLYTYPE/K,FILESONLY/S,DIRSONLY/S,QUIET/S,FLOAT/S,OVERWRITE/S,ALL/S";
  26. char *CliHelp = "AddIcon V"VERSIONREVISION" © 1992-94 S.R. & P.C.";
  27. static char Version[] = VERSTAG;
  28.  
  29. #define ARG_FileName 0
  30. #define ARG_ONLY        1
  31. #define ARG_FILES        2
  32. #define ARG_DIRS        3
  33. #define ARG_QUIET        4
  34. #define ARG_FLOAT        5
  35. #define ARG_OVERWRITE    6
  36. #define ARG_ALL            7
  37. #define ARG_ENDARG        8
  38.  
  39.  
  40. static void GetDefIconsDir(char *Buf)
  41. {
  42.     #define DOSLISTFLAGS    (LDF_READ|LDF_VOLUMES|LDF_ASSIGNS)
  43.     struct DosList *dl;
  44.     BOOL Done = FALSE;
  45.  
  46.     if (dl = LockDosList(DOSLISTFLAGS)) {
  47.         if (FindDosEntry(dl, "ICONS", DOSLISTFLAGS)) {
  48.             strcpy(Buf, "ICONS:");
  49.             Done = TRUE;
  50.         }
  51.         UnLockDosList(DOSLISTFLAGS);
  52.     }
  53.     if (!Done)
  54.         strcpy(Buf, "ENV:Sys");
  55. }
  56.  
  57.  
  58. /* FIB can be null but give it if you alredy have it. */
  59.  
  60. BOOL AddIcon(char *FileName, FileType OnlyType, BOOL Quiet, BOOL Float, BOOL Overwrite, struct DiskObject ** LastDOb, FileType * LastType, struct FileInfoBlock * FIB)
  61. {
  62.     char *IconName = NULL;
  63.     char Buf[255];
  64.     FileType Type;
  65.     LONG WBType = 0;
  66.     BOOL Idem;
  67.     BOOL Ok = FALSE;    /* assume the worst */
  68.     struct DiskObject *dob = NULL;
  69.     BPTR L;
  70.  
  71.     if (!Quiet) {
  72.         Printf("%-32s  ", FileName);
  73.         Flush(Output());
  74.     }
  75.     SPrintf(Buf, "%s.info", FileName);
  76.     if (!Overwrite && (L = Lock(Buf, ACCESS_READ))) {
  77.         UnLock(L);
  78.         if (!Quiet)
  79.             PutStr("Icon already exist.\n");
  80.         return TRUE;
  81.     }
  82.     Type = WhatIsTags(FileName, WI_Deep, DEEPTYPE, (FIB) ? WI_FIB : TAG_IGNORE, FIB, TAG_DONE);
  83.     if (!Quiet) {
  84.         Printf("%-12s ", GetIDString(Type));
  85.         Flush(Output());
  86.     }
  87.     if ((OnlyType != TYPE_UNSCANNED) && CmpFileType(Type, OnlyType)) {
  88.         if (!Quiet)
  89.             PutStr("Type not desired\n");
  90.         return TRUE;
  91.     }
  92.     Idem = !CmpFileType(Type, *LastType);
  93.  
  94.     if (CmpFileType(Type, TYPE_UNKNOWNFILETYPE)) {
  95.         if (!CmpFileType(Type, GetIDType("Volume"))) {
  96.             IconName = "def_Disk";
  97.             FileName = "Disk";
  98.             WBType = WBDISK;    /* To use ROM resident icon if no def_Tool defined in ENV:Sys */
  99.         }
  100.         else if (!CmpFileType(Type, GetIDType("Dir"))) {
  101.             IconName = "def_Drawer";
  102.             WBType = WBDRAWER;    /* To use ROM resident icon if no def_Tool defined in ENV:Sys */
  103.         }
  104.         else if (IconName = GetIconName(Type)) {
  105.             if (!Stricmp(IconName, "def_Tool"))
  106.                 WBType = WBTOOL;    /* To use ROM resident icon if no def_Tool defined in ENV:Sys */
  107.             else if (!Stricmp(IconName, "def_Project"))
  108.                 WBType = WBPROJECT;    /* To use ROM resident icon if no def_Project defined in ENV:Sys */
  109.         }
  110.         if (!Quiet) {
  111.             if (IconName) {
  112.                 Printf("%s...", IconName);
  113.                 Flush(Output());
  114.             }
  115.             else
  116.                 Printf("No icon defined!\n");
  117.         }
  118.         GetDefIconsDir(Buf);
  119.         AddPart(Buf, IconName, 50);        // Use AddPart() so that user can configure DEFICONS:def_Mumble is S:FileTypes
  120.         if ((dob = (Idem) ? *LastDOb : ((WBType) ? GetDefDiskObject(WBType) : GetDiskObject(Buf)))) {
  121.             if (Float) {
  122.                 dob->do_CurrentX = NO_ICON_POSITION;
  123.                 dob->do_CurrentY = NO_ICON_POSITION;
  124.             }
  125.             if (PutDiskObject(FileName, dob)) {
  126.                 if (!Quiet)
  127.                     PutStr("Ok\n");
  128.                 if (!Idem) {
  129.                     if (*LastDOb)
  130.                         FreeDiskObject(*LastDOb);
  131.                     *LastDOb = dob;    /* memorise the current icon */
  132.                     *LastType = Type;    /* memorise the current type */
  133.                 }
  134.                 Ok = TRUE;
  135.             }
  136.             else
  137.                 PutStr("Can't write icon !\n");
  138.         }
  139.         else if (IconName)
  140.             PutStr("Can't read icon.\n");
  141.     }
  142.     else
  143.         PutStr("Unknown file type.\n");
  144.     return Ok;
  145. }
  146.  
  147.  
  148. long Main(char *argv[], struct WBStartup *WBenchMsg)
  149. {
  150.     long rc = 0, NumSub = 0;
  151.     char *FileName;
  152.     BOOL Files, Dirs, Quiet, Float, OverWrite, All;
  153.     struct AnchorPath *AP;
  154.  
  155.     FileName = argv[ARG_FileName];
  156.     Files = (BOOL) argv[ARG_FILES];
  157.     Dirs = (BOOL) argv[ARG_DIRS];
  158.     Quiet = (BOOL) argv[ARG_QUIET];
  159.     Float = (BOOL) argv[ARG_FLOAT];
  160.     OverWrite = (BOOL) argv[ARG_OVERWRITE];
  161.     All = (BOOL) argv[ARG_ALL];
  162.  
  163.     if (!(IconBase = OpenLibrary("icon.library", 37L)))
  164.         return 20;
  165.     if (WhatIsBase = OpenLibrary("whatis.library", 0)) {
  166.         if (AP = AllocVec(sizeof(struct AnchorPath) + 256, MEMF_PUBLIC|MEMF_CLEAR)) {
  167.             struct DiskObject *LastDOb = NULL;
  168.             FileType LastType = TYPE_UNKNOWNFILETYPE, OnlyType = TYPE_UNSCANNED;
  169.             LONG MatchErr;
  170.             BPTR StartDir;
  171.  
  172.             if (argv[ARG_ONLY])
  173.                 OnlyType = GetIDType(argv[ARG_ONLY]);
  174.             AP->ap_BreakBits = SIGBREAKF_CTRL_C;    /* Break on these bits    */
  175.             AP->ap_Strlen = 255;
  176.             AP->ap_Flags = (All) ? APF_DODOT : APF_DODOT|APF_DOWILD;    /* allow convertion of '.' to CurrentDir */
  177.  
  178.             switch (ParsePatternNoCase(FileName, AP->ap_Buf, 256)) {
  179.             case 1:        // Pattern
  180.                 StartDir = CurrentDir(((struct Process *) SysBase->ThisTask)->pr_CurrentDir);
  181.                 for (MatchErr = MatchFirst(FileName, AP); MatchErr == 0; MatchErr = MatchNext(AP)) {
  182.                     if (AP->ap_Flags & APF_DirChanged) {
  183.                         CurrentDir(AP->ap_Current->an_Lock);
  184.                     }
  185.                     if (AP->fib_DirEntryType > 0) {
  186.                         if (AP->ap_Flags & APF_DIDDIR) {
  187.                             if (!Files)
  188.                                 AddIcon(AP->fib_FileName, OnlyType, Quiet, Float, OverWrite, &LastDOb, &LastType, &AP->ap_Info);
  189.                             /* clear the completed directory flag */
  190.                             AP->ap_Flags &= ~APF_DIDDIR;
  191.                             NumSub--;
  192.                         }
  193.                         else {
  194.                             if (All) {
  195.                                 long i;
  196.  
  197.                                 if (!Quiet) {
  198.                                     for (i=0; i<NumSub; i++)
  199.                                         Printf("    ");
  200.                                     Printf("%s  (dir)\n", AP->fib_FileName);
  201.                                 }
  202.                                 /* make it enter the directory */
  203.                                 AP->ap_Flags |= APF_DODIR;
  204.                                 NumSub++;
  205.                             }
  206.                             else
  207.                                 AddIcon(AP->fib_FileName, OnlyType, Quiet, Float, OverWrite, &LastDOb, &LastType, &AP->ap_Info);
  208.                         }
  209.                     }
  210.                     else {        // File
  211.                         if (!Dirs && !MatchPatternNoCase("\X80.INFO", AP->fib_FileName))
  212.                             AddIcon(AP->fib_FileName, OnlyType, Quiet, Float, OverWrite, &LastDOb, &LastType, &AP->ap_Info);
  213.                     }
  214.                 }
  215.                 MatchEnd(AP);    /* This absolutely, positively must be called, all of the time. */
  216.                 CurrentDir(StartDir);
  217.                 if (MatchErr != ERROR_NO_MORE_ENTRIES) {
  218.                     PrintFault(MatchErr, "AddIcon");
  219.                     rc = RETURN_ERROR;
  220.                 }
  221.                 break;
  222.             case 0:
  223.                 // Normal file or dir, no pattern.
  224.                 if (!MatchPatternNoCase("\X80.INFO", AP->fib_FileName))
  225.                     AddIcon(FileName, OnlyType, Quiet, Float, OverWrite, &LastDOb, &LastType, NULL);
  226.                 break;
  227.             default:
  228.                 PrintFault(IoErr(), "AddIcon");
  229.                 rc = RETURN_ERROR;
  230.             }
  231.             if (LastDOb)
  232.                 FreeDiskObject(LastDOb);
  233.             FreeVec(AP);
  234.         }
  235.         CloseLibrary(WhatIsBase);
  236.     }
  237.     else
  238.         Printf("Requires whatis.library\n");
  239.     CloseLibrary(IconBase);
  240.     return rc;
  241. }
  242.  
  243.